//
//  WYPurseDetailController.m
//  Boss
//
//  Created by admin on 2018/7/2.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYPurseDetailController.h"
#import "WYMoneyCell.h"
#import "WYWalletDetail.h"
@interface WYPurseDetailController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,assign)BOOL isRefresh;
@property (nonatomic,strong) NSString *type;
@property (nonatomic,assign)NSUInteger page;
@end

@implementation WYPurseDetailController

-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    self.page = 1;
    self.type = @"";
    
    [self loadDataWithType:self.type];
    
    WEAKSELF
    self.tableView.mj_footer=[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.isRefresh=NO;
        weakSelf.page++;
        // [weakSelf loadTableData];
        [weakSelf loadDataWithType:weakSelf.type];
    }];
    
    
    
    
    
    
    
}
-(void)loadDataWithType:(NSString *)type
{
     NSString *page = [NSString stringWithFormat:@"%ld",self.page];
    WEAKSELF
    [WYHttpServer getUserWalletRecord:@"15" WithCurrentPage:page WithType:type SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        if (success) {
            if (weakSelf.isRefresh) {
                [weakSelf.dataArray removeAllObjects];
            }
            
            [weakSelf.dataArray addObject:[WYWalletDetail getWalletDetailListFromJson:response[@"result"][@"detail"]]];
            [weakSelf.tableView reloadData];
            if ([response[@"result"][@"hasNextPage"] integerValue]) {
                [weakSelf.tableView.mj_footer endRefreshing];
            }else{
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            
            
            
        }
        
        
        
    }];
    
}

-(void)setupUI
{
    [self.view addSubview:self.tableView];
    self.navigationItem.title = @"收支明细";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"筛选" style:(UIBarButtonItemStyleDone) target:self action:@selector(screenAction)];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"WYMoneyCell" bundle:nil] forCellReuseIdentifier:@"WYMoneyCell"];
    self.tableView.rowHeight = 75;
    self.tableView.tableFooterView = [[UIView alloc]init];
    [self.tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
}
-(void)screenAction
{
    WEAKSELF
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:(UIAlertControllerStyleActionSheet)];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        weakSelf.isRefresh = YES;
        weakSelf.type = @"";
        [weakSelf loadDataWithType:weakSelf.type];
    }];
    UIAlertAction *action4 = [UIAlertAction actionWithTitle:@"全部" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        weakSelf.isRefresh = YES;
        weakSelf.type = @"";
        [weakSelf loadDataWithType:weakSelf.type];
        
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"收入" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        weakSelf.isRefresh = YES;
        weakSelf.type = @"1";
        [weakSelf loadDataWithType:weakSelf.type];
        
    }];
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"支出" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        weakSelf.isRefresh = YES;
        weakSelf.type = @"2";
        [weakSelf loadDataWithType:weakSelf.type];
    }];
   
    [alertController addAction:action1];
    [alertController addAction:action4];
    [alertController addAction:action2];
    [alertController addAction:action3];
    if (IS_IPAD) {
        UIPopoverPresentationController *popPresenter = [alertController popoverPresentationController];
        popPresenter.barButtonItem = self.navigationItem.rightBarButtonItem;
//        popPresenter.sourceRect = cell.bounds;
        [self presentViewController:alertController animated:YES completion:nil];
        
    }else{
        [self presentViewController:alertController animated:YES completion:nil];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataArray[section] count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    WYMoneyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WYMoneyCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"WYMoneyCell" owner:nil options:nil].firstObject;
    }
    WYWalletDetail *detail = self.dataArray[indexPath.section][indexPath.row];
    [cell setSelectionStyle:(UITableViewCellSelectionStyleNone)];
    
    [cell setDetail:detail];
    
    
    
    return cell;
    
    //return nil;
}






- (UITableView *)tableView
{
    if (_tableView == nil) {
        CGRect frame = CGRectMake(0, self.navHeight, kScreenWidth, kScreenHeight-self.navHeight);
        _tableView = [[UITableView alloc] initWithFrame:frame
                                                  style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}


@end
